# -*- coding: utf-8 -*-
import logging
import datetime
import time
from threading import Thread
from time import sleep, strftime, mktime
from bs4 import BeautifulSoup

from ibapi.client import EClient
from ibapi.wrapper import EWrapper

from article import Article
from article_storage import ArticleCSVStorage, ArticleMYSQLStorage

news = {}
storage = {}

class IBNewsApp(EClient, EWrapper):
    def __init__(self):
        EWrapper.__init__(self)
        EClient.__init__(self, wrapper=self)
        self.requestid = 2000

    def nextValidId(self, orderId:int):
        print("Next Valid Id:", orderId)
        self.requestid = self.requestid + orderId

    def getNextId(self):
        return self.requestid

    def historicalNews(self, requestId:int, time:str, providerCode:str, articleId:str, headline:str):
        print("Historical News:", requestId, time, providerCode, articleId, headline)
        self.requestid += 1

        news[self.requestid] = {
            "time": time,
            "providerCode": providerCode,
            "articleId": articleId,
            "headline": headline
        }
        self.reqNewsArticle(self.requestid, providerCode= providerCode, articleId=articleId, newsArticleOptions=[])

    def historicalNewsEnd(self, requestId:int, hasMore:bool):
        # print("Historical News End:", requestId, hasMore)
        self.cancelHistoricalData(requestId)
        # pass

    def newsArticle(self, requestId:int, articleType:int, articleText:str):
        # print("News Article:", requestId,articleType, articleText)
        it = news[requestId]
        if articleText.startswith("<html>") or it['providerCode'] == 'BZ':
            # text2 = html.unescape(articleText)
            # print(text2)
            parser = BeautifulSoup(articleText, "html.parser")
            text = parser.getText()
            # text = articleText
            # print("\n\n",text)
            # print(html.unescape( "&#169;, &#8217;\n" ))
        else:
            arr = articleText.split("&#10;")
            issueIdx = -1
            copyrightIdx = -1
            for i in range(len(arr)):
                item = arr[i]
                if type(item) != str:
                    continue
                b = item.startswith("Issuance", 0)
                if b:
                    issueIdx = i
            if issueIdx >= 0:
                arr.pop(issueIdx)
            for i in range(len(arr)):
                item = arr[i]
                if type(item) != str:
                    continue
                b = item.startswith("Copyright", 0)
                if b:
                    copyrightIdx = i
            if copyrightIdx >= 0:
                arr.pop(copyrightIdx)
            text = ''.join(arr)
        t = time.strptime(it['time'], "%Y-%m-%d %H:%M:%S.0")
        t1 = strftime("%Y-%m-%d", t)
        t2 = strftime("%H:%M:%S", t)
        article = Article(article_id=it['articleId'],symbol="AAPL", article_date=t1, article_time = t2, provider_code= it['providerCode'], headline=it['headline'], article_text= text)
        dt = datetime.datetime.fromtimestamp(mktime(t))
        week = dt.isocalendar()[1]
        storage = ArticleMYSQLStorage()
        it = storage.getArticle(article.article_id)
        if it is not None:
            return
        storage.save(article)


if __name__ == "__main__":
    app = IBNewsApp()
    app.connect("127.0.0.1", 7497, 0)

    thread = Thread(target=app.run)
    thread.start()
    time.sleep(2)

    # 获取最近2年的数据
    start = datetime.datetime.now() + datetime.timedelta(days=-190)
    time_format = "%Y-%m-%d 23:59:00"
    begin = start
    for i in range(0, (datetime.datetime.now() - begin).days, 7):
        print(i)
        begin_str = begin.strftime(time_format)
        end = start + datetime.timedelta(days=i+7)
        now = datetime.datetime.now()
        if end > now:
            end = now
        end_str = end.strftime(time_format)
        # end_str = "2024-07-15 23:59:00"
        print("begin_str:", begin_str, "end_str:", end_str)
        app.reqHistoricalNews(app.getNextId(), 265598, "BRFG+BRFUPDN+BZ+DJNL", startDateTime=end_str, endDateTime=begin_str, totalResults=300, historicalNewsOptions=[])
        begin = end
        time.sleep(10)
        # break
